package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbAddressService;
import com.dongdongshop.vo.AddressVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yaml.snakeyaml.events.Event;

import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {
    @DubboReference(check = false)
    private TbAddressService addressService;


    @RequestMapping("getAddress")
    @ResponseBody
    private Result getAddress(){
        try {
            Subject subject = SecurityUtils.getSubject();
            TbUser user = (TbUser) subject.getPrincipal();
            if(user == null){
                return new Result(999,"请先登录");
            }
            List<AddressVo> addressList = addressService.selectAddressByUserId(user.getUsername());
            return Result.OK().setData(addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @RequestMapping("addUserAdd")
    @ResponseBody
    public Result addUserAdd(TbAddress address) {
        try {
            Subject subject = SecurityUtils.getSubject();
            TbUser user = (TbUser) subject.getPrincipal();
            if(user == null){
                return new Result(999,"请先登录");
            }
            String username = user.getUsername();
            address.setUserId(username);
            address.setIsDefault("0");
            addressService.addAddress(address);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id) {
        try {
            TbAddress address = addressService.selectAddressById(id);
            return Result.OK().setData(address);
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }

    @RequestMapping("updateAddress")
    @ResponseBody
    public Result updateAddress(TbAddress address) {
        try {
            addressService.updateAddressById(address);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
    @RequestMapping("toUpdateArea")
    @ResponseBody
    public Result toUpdateArea(Long id) {
        try {
            addressService.toUpdateArea(id);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }

    @RequestMapping("deleteAddress")
    @ResponseBody
    public Result deleteAddress(Long id) {
        try {
            addressService.deleteAddressById(id);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }



    @RequestMapping("selectOne")
    @ResponseBody
    public Result select() {
        try {
            List<TbProvinces> provincesList = addressService.selectProvinces();
            return Result.OK().setData(provincesList);
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
    @RequestMapping("selectTwo")
    @ResponseBody
    public Result selectTwo(String id) {
        try {
            List<TbCities> citiesList = addressService.selectCities(id);
            return Result.OK().setData(citiesList);
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
    @RequestMapping("selectThree")
    @ResponseBody
    public Result selectThree(String id) {
        try {
            List<TbAreas> areasList = addressService.selectAreas(id);
            return Result.OK().setData(areasList);
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }

    @RequestMapping("toHomeIndex")
    public String toHomeIndex(){
        return "home-index";
    }
    @RequestMapping("toHomeSettingInfo")
    public String toHomeSettingInfo(){
        return "home-setting-info";
    }
    @RequestMapping("toSettingAddress")
    public String toSettingAddress(){
        return "home-setting-address";
    }
    @RequestMapping("toSend")
    public String toSend(){
        return "home-order-send";
    }
}
